﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Neuro;

namespace Projekt1SN
{
    public static class FileReader
    {
        private static INeuralNetwork proccessKohonenConfig(string[] configStrings)
        {
            configStrings[1] = configStrings[1].Trim();
            string[] neuronNumbers = configStrings[1].Split(new char[] { ' ' }, int.MaxValue);
            int[] neuronNumbersParsed = new int[neuronNumbers.Length];
            for (int i = 0; i < neuronNumbersParsed.Length; i++)
            {
                neuronNumbersParsed[i] = int.Parse(neuronNumbers[i]);
            }

            configStrings[1] = configStrings[2].Trim();
            string[] learningNumbers = configStrings[2].Split(new char[] { ' ' }, int.MaxValue);
            double[] learningNumbersParsed = new double[learningNumbers.Length];

            for (int i = 0; i < learningNumbersParsed.Length; i++)
            {
                learningNumbers[i] = learningNumbers[i].Replace('.', ',');
                learningNumbersParsed[i] = double.Parse(learningNumbers[i]);
            }


            return new KohonenNetwork(  neuronNumbersParsed[0], neuronNumbersParsed[1], neuronNumbersParsed[2], 
                                        (int)learningNumbersParsed[0], (int)learningNumbersParsed[1], learningNumbersParsed[2], 
                                        learningNumbersParsed[3], learningNumbersParsed[4]);
        }

        private static FeedForwardNetwork proccessFeedForwardConfig(string[] configStrings)
        {
            //MessageBox.Show(config);
            bool bias = false;
            bool bipolar = false;

            // bias or nobias?
            if (configStrings[1].IndexOf("bias") != -1 && configStrings[1].IndexOf("nobias") == -1)
            {
                bias = true;
            }
            else if (configStrings[1].IndexOf("nobias") != -1)
            {
                bias = false;
            }
            else
            {
                MessageBox.Show("Bad file format! In second line use bias/nobias option!", "Bad File!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

            // unipolar or bipolar?
            if (configStrings[1].IndexOf("bipolar") != -1)
            {
                bipolar = true;
            }
            else if (configStrings[1].IndexOf("unipolar") != -1)
            {
                bipolar = false;
            }
            else
            {
                MessageBox.Show("Bad file format! In second line use unipolar/bipolar option!", "Bad File!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

            configStrings[2] = configStrings[2].Trim();
            string[] neuronNumbers = configStrings[2].Split(new char[] {' '}, int.MaxValue);
            int[] neuronNumbersParsed = new int[neuronNumbers.Length - 1];
            for (int i = 0; i < neuronNumbersParsed.Length; i++)
            {
                neuronNumbersParsed[i] = int.Parse(neuronNumbers[i + 1]);
            }

            configStrings[3] = configStrings[3].Trim();
            string[] learningNumbers = configStrings[3].Split(new char[] { ' ' }, int.MaxValue);
            double[] learningNumbersParsed = new double[learningNumbers.Length];

            
            for (int i = 0; i < learningNumbersParsed.Length; i++)
            {
                learningNumbers[i] = learningNumbers[i].Replace('.', ',');
                learningNumbersParsed[i] = double.Parse(learningNumbers[i]);
            }

           
            
            return new FeedForwardNetwork(bipolar ? ActivationFunctionType.Bipolar : ActivationFunctionType.Unipolar, bias, 
                                            int.Parse(neuronNumbers[0]), 
                                            neuronNumbersParsed, 
                                            learningNumbersParsed[1], 
                                            learningNumbersParsed[2], 
                                            (int)learningNumbersParsed[0]);
        }

        public static INeuralNetwork ProcessConfigFile(string[] configStrings)
        {
            string networkType = configStrings[0];

            if (networkType == null)
            {
                MessageBox.Show("Bad file format! First line is empty!", "Bad File!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            if (networkType.Contains("feedforward"))
            {
                return proccessFeedForwardConfig(configStrings);
            }
            if (networkType.Contains("kohonen"))
            {
                return proccessKohonenConfig(configStrings);
            }
            MessageBox.Show("Bad file format! First have to contain network Type(feedforwad or kohonen)!", "Bad File!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
        }
    }
}
